package com.lemon.smartsafety.ui.adapter.main;

import android.view.View;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.entity.event.main.MainAdEvent;
import com.lemon.smartsafety.entity.main.MainRcvItemBase;
import com.lemon.smartsafety.transforms.DefaultTransformer;
import com.lemon.smartsafety.ui.adapter.common.RcvViewType;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.view.banner.NetworkImageHolderView;
import com.lemon.safecloud.greendao.PlayView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/2/1.
 */
public class MainAdItemView implements AdapterItem<MainRcvItemBase<List<PlayView>>> {
    private LoggerHelper loggerHelper = new LoggerHelper(MainAdItemView.class);
    @Bind(R.id.banner)
    ConvenientBanner mBanner;

    @Override
    public int getLayoutResId() {
        return R.layout.item_fragment_banner;
    }

    @Override
    public void bindViews(View root) {
        ButterKnife.bind(this, root);
    }

    @Override
    public void setViews() {
    }

    @Override
    public void handleData(MainRcvItemBase<List<PlayView>> mainRcvItemBase, int position) {
        if (RcvViewType.MainRcvViewType.TYPE_AD != mainRcvItemBase.getViewType()) {
            return;
        }
        List<PlayView> playViews = mainRcvItemBase.getData();

        if (playViews != null) {
            final List<PlayView> views = new ArrayList<PlayView>();
            for (int i = 0; i < playViews.size() && i < Constants.MAX_BANNER_IMAGE_COUNT; i++) {
                views.add(playViews.get(i));
            }
            mBanner.stopTurning();
            mBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
                @Override
                public NetworkImageHolderView createHolder() {
                    return new NetworkImageHolderView();
                }
            }, views)//设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                   .setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused})
                   //设置指示器的方向
                   .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
            if (playViews.size() != 1) {
                //设置翻页的效果，不需要翻页效果可用不设
                mBanner.setPageTransformer(new DefaultTransformer()).startTurning(Constants.BANNER_TURNING_DELAY);
            }
            mBanner.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    MainAdEvent event = new MainAdEvent();
                    event.setTag(EventTag.TAG_MAIN_PANEL_AD);
                    event.setIndex(position);
                    event.setPlayView(views.get(position));
                    AppService.getBus().post(event);
                }
            });
        }
    }

}
